package com.java.business.system.permis.web;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.java.business.system.permis.bean.User;
import com.java.business.system.permis.service.UserService;
import com.java.core.bean.BaseController;
import com.java.core.bean.ResultBean;
import com.java.core.utils.ResponseUtils;

@RequestMapping("/system/permis/user")
@Controller
public class UserController extends BaseController {
	@Autowired
	private UserService service;

	@RequestMapping("/add")
	public void add(HttpServletResponse response, User user) {
		ResultBean rb = new ResultBean();
		try {
			service.add(user);
		} catch (Exception e) {
			rb.setSuccess(false);
			rb.setMsg(e.getMessage());
		}
		ResponseUtils.renderJson(response, JSON.toJSONString(rb));
	}

	@RequestMapping("/update")
	public void update(HttpServletResponse response, User user) {
		ResultBean rb = new ResultBean();
		try {
			service.update(user);
		} catch (Exception e) {
			rb.setSuccess(false);
			rb.setMsg(e.getMessage());
		}
		ResponseUtils.renderJson(response, JSON.toJSONString(rb));
	}

	@RequestMapping("/delete")
	public void delete(HttpServletResponse response, User user) {
		ResultBean rb = new ResultBean();
		try {
			service.delete(user);
		} catch (Exception e) {
			rb.setSuccess(false);
			rb.setMsg(e.getMessage());
		}
		ResponseUtils.renderJson(response, JSON.toJSONString(rb));
	}

	@RequestMapping("/setPassword")
	public void setPassword(HttpServletResponse response, User user) {
		ResultBean rb = new ResultBean();
		try {
			service.setPassword(user);
		} catch (Exception e) {
			rb.setSuccess(false);
			rb.setMsg(e.getMessage());
		}
		ResponseUtils.renderJson(response, JSON.toJSONString(rb));
	}

	@RequestMapping("/setDisabled")
	public void setDisabled(HttpServletResponse response, User user) {
		ResultBean rb = new ResultBean();
		try {
			service.setDisabled(user);
		} catch (Exception e) {
			rb.setSuccess(false);
			rb.setMsg(e.getMessage());
		}
		ResponseUtils.renderJson(response, JSON.toJSONString(rb));
	}

	@RequestMapping("/setRoles")
	public void setRoles(HttpServletResponse response, String userid,
			String[] roleids) {
		ResultBean rb = new ResultBean();
		try {
			service.setRoles(userid, roleids);
		} catch (Exception e) {
			rb.setSuccess(false);
			rb.setMsg(e.getMessage());
		}
		ResponseUtils.renderJson(response, JSON.toJSONString(rb));
	}

	@RequestMapping("/setRole")
	public void setRole(HttpServletResponse response, String userid,
			String roleid) {
		ResultBean rb = new ResultBean();
		try {
			service.setRole(userid, roleid);
		} catch (Exception e) {
			rb.setSuccess(false);
			rb.setMsg(e.getMessage());
		}
		ResponseUtils.renderJson(response, JSON.toJSONString(rb));
	}

	@RequestMapping("/removeRole")
	public void removeRole(HttpServletResponse response, String userid,
			String roleid) {
		ResultBean rb = new ResultBean();
		try {
			service.removeRole(userid, roleid);
		} catch (Exception e) {
			rb.setSuccess(false);
			rb.setMsg(e.getMessage());
		}
		ResponseUtils.renderJson(response, JSON.toJSONString(rb));
	}

	@RequestMapping("/getUserList")
	public void getUserList(HttpServletResponse response, User user) {
		ResultBean rb = new ResultBean();
		try {
			List<User> list = service.getUserList(user);
			rb.setData(list);
		} catch (Exception e) {
			rb.setSuccess(false);
			rb.setMsg(e.getMessage());
			e.printStackTrace();
		}
		ResponseUtils.renderJson(response, JSON.toJSONString(rb));
	}
}
